package oneArmedBandit.theGame;

import java.util.EventListener;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class OneArmedBanditActivity extends Activity implements SlotEventListener
{
	Timer scheduler;
	Handler handling = new Handler(){
    	@Override
    	public void handleMessage(android.os.Message msg) 
    	{
    		isTheSame(((ImageView)msg.obj), msg.what);
    		((TextView)findViewById(R.id.textView1)).setText(1); 
    	};
    };
    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scheduler = new Timer();
        
        ((Button)findViewById(R.id.buttonStart)).setOnClickListener(new OnClickListener() 
		        {	
					@Override
					public void onClick(View v) {
						if(money>0)
						{
					        ((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot1)).getBackground()).start();
					        
							((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot2)).getBackground()).start();
					        
							((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot3)).getBackground()).start();
							
					        scheduler.schedule(new TimerTask() {
								
					        	
					            
								@Override
								public void run() {
									((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot1)).getBackground()).stop();
									ImageView view = ((ImageView)findViewById(R.id.imageViewSlot1));
									Message msg = new Message();
									msg.arg1 = 1;
									msg.obj = view;
									handling.sendMessage(msg);
									//isTheSame(view,1);

				    				//((TextView)findViewById(R.id.textView1)).setText(1); 
									
								}
								
								
							}, 4000);
					        
					        scheduler.schedule(new TimerTask() {
								
								@Override
								public void run() {
									((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot2)).getBackground()).stop();
									ImageView view = ((ImageView)findViewById(R.id.imageViewSlot2));

									isTheSame(view,2);
									
								}
							}, 8000);
					        
					        scheduler.schedule(new TimerTask() {
								
								@Override
								public void run() {
									((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot3)).getBackground()).stop();
									ImageView view = ((ImageView)findViewById(R.id.imageViewSlot3));

									isTheSame(view,3);
									
								}
							}, 12000);
							
							slot1 = slot2 = slot3 = true;
							win = false;
							value1 = value2 = value3 = 0;
							money--;
			    			repaint();
			    			((Button)findViewById(R.id.buttonStart)).setEnabled(false);
						}
						else
						{
							((Button)findViewById(R.id.buttonStart)).setEnabled(false);
						}
					}
				});
        
        ((Button)findViewById(R.id.buttonSlot1)).setOnClickListener(new OnClickListener() 
		        {	
					@Override
					public void onClick(View v) {
						((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot1)).getBackground()).stop();
						ImageView view = ((ImageView)findViewById(R.id.imageViewSlot1));
		
						isTheSame(view,1);
						;
					}
				});
		        
        ((Button)findViewById(R.id.buttonSlot2)).setOnClickListener(new OnClickListener() 
        		{	
					@Override
					public void onClick(View v) {
						((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot2)).getBackground()).stop();
						ImageView view = ((ImageView)findViewById(R.id.imageViewSlot2));
		
						isTheSame(view,2);
					}
				});
        
        ((Button)findViewById(R.id.buttonSlot3)).setOnClickListener(new OnClickListener() 
		        {	
					@Override
					public void onClick(View v) {
						((AnimationDrawable) ((ImageView)findViewById(R.id.imageViewSlot3)).getBackground()).stop();
						ImageView view = ((ImageView)findViewById(R.id.imageViewSlot3));
						
						isTheSame(view,3);
					}
				});

		repaint();
    }
    
    boolean slot1 = true;
    boolean slot2 = true;
    boolean slot3 = true;
    int value1 = 0;
    int value2 = 0;
    int value3 = 0;
    boolean win = false;
    
    int money = 20;
    
    public synchronized void isTheSame(ImageView draw, int slot)
    {
    	switch(slot)
    	{
    		case 1:
    			if(slot1)
    			{
    				slot1 = false;
    				value1 = compare(draw);
    				((TextView)findViewById(R.id.textView1)).setText(Integer.toString(value1));
    			}
    			break;
    		case 2:
    			if(slot2)
    			{
    				slot2 = false;
    				value2 = compare(draw);
    				((TextView)findViewById(R.id.textView2)).setText(Integer.toString(value2));
    			}
    			break;
    		case 3:
    			if(slot3)
    			{
    				slot3 = false;
    				value3 = compare(draw);
    				((TextView)findViewById(R.id.textView3)).setText(Integer.toString(value3));
    			}
    			break;
    		default:
    	}
    	
    	if(!slot1 && !slot2 && !slot3)
    	{
    		if(value1 == value2 && value2 == value3 && value1 == value3)
    		{
    			money = money + 100;
    			win = true;
    		}
    		else if(value1 == value2 || value2 == value3 || value1 == value3)
    		{
    			money = money + 20;
    			win = true;
    		}

			((Button)findViewById(R.id.buttonStart)).setEnabled(true);
			repaint();
    	}
    }
    
    private int compare(ImageView view)
    { 
		Drawable draw = ((AnimationDrawable) view.getBackground()).getCurrent();
		
    	if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(0)))
    		return 1;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(1)))
    		return 2;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(2)))
    		return 3;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(3)))
    		return 4;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(4)))
    		return 5;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(5)))
    		return 6;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(6)))
    		return 7;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(7)))
    		return 8;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(8)))
    		return 9;
    	else if(draw.equals(((AnimationDrawable) view.getBackground()).getFrame(9)))
    		return 10;
    	else
    		return 0;
    }
    
    private void repaint()
    {
    	((TextView)findViewById(R.id.WinningValue)).setText(Integer.toString(money));
    }

	@Override
	public void slotEventOccured(SlotEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	
}